package com.mud.mudcommune.view.fragment.threeLevel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.CaseContentResponse;
import com.mud.mudcommune.entry.CaseEntry;
import com.mud.mudcommune.entry.CaseGalleryEntry;
import com.mud.mudcommune.utils.LogUtils;
import com.mud.mudcommune.http.ApiService;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/2/23 0023.
 */

public class HotRecommendDetailsFragment extends Fragment implements Callback<CaseContentResponse> {
    private static final String TAG = HotRecommendDetailsFragment.class.getSimpleName();
    private static Fragment fragment = new HotRecommendDetailsFragment();
    private Unbinder unbinder;
    @BindView(R.id.case_back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.details_style)
    TextView style;
    @BindView(R.id.details_space)
    TextView space;
    @BindView(R.id.details_area)
    TextView area;
    @BindView(R.id.details_like)
    TextView like;
    @BindView(R.id.head_img)
    ImageView image;
    @BindView(R.id.desc)
    TextView desc;
    @BindView(R.id.vr)
    ImageView vr;
    @BindView(R.id.loading_layout)
    LinearLayout loadingLayout;

    public static Fragment getInstance() {
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_case_detials, container, false);
        unbinder = ButterKnife.bind(this, view);
        requestDate();
        return view;
    }

    @OnClick({R.id.vr, R.id.case_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.vr:
//                getFragmentManager().beginTransaction()
//                        .hide(CaseDetailsFragment.fragment)
//                        .add(R.id.main_frame, mWebViewFragment)
//                        .addToBackStack(null)
//                        .commit();
                break;
            case R.id.case_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    // 请求数据
    private void requestDate() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Bundle args = this.getArguments();
        int caseId = args.getInt("caseId");//VR跳转过来的页面没有传值
        Call<CaseContentResponse> caseTypeResponse = apiService.getCaseDetails(caseId);
        caseTypeResponse.enqueue(this);
        LogUtils.i(TAG, caseId + "   caseId");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
       // unbinder.unbind();
    }

    @Override
    public void onResponse(Call<CaseContentResponse> call, Response<CaseContentResponse> response) {
        loadingLayout.setVisibility(View.GONE);
        CaseEntry caseEntry = response.body().getCaseContentList().get(0);
        if (caseEntry != null) {
            title.setText(caseEntry.getCaseName() + "");
            style.setText("风格:");
            space.setText("空间:");
            area.setText("面积:" + caseEntry.getArea() + "");
            like.setText("喜欢:" + caseEntry.getLike() + "");
            List<CaseGalleryEntry> caseGalleryList = caseEntry.getCaseGalleryList();
            if (caseGalleryList != null && caseGalleryList.size() > 0 && image != null) {
                Glide.with(getActivity())
                        .load(caseEntry.getCase_img())
                        .into(image);
            }
            desc.setText(caseEntry.getCaseDesc());
        }
    }

    @Override
    public void onFailure(Call<CaseContentResponse> call, Throwable t) {
        t.printStackTrace();
    }
}

